package com.cloud.drore.eboss.supplier.mapper.provider;


import com.alibaba.fastjson.JSONObject;
import com.cloud.drore.eboss.supplier.entity.po.ProductInfoResponsePo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

public class ProductProvider {
    private static Logger logger = LoggerFactory.getLogger(ProductProvider.class);

    public ProductProvider() {
    }


    /**
     * @ClassName: ProductProvider
     * @param map
     * @Return: java.lang.String
     * @Decription: 畅游通公共产品修改的集合
     * @CreateDate: Created in 2018/3/16 0016 下午 1:28
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public String updateProductAll(Map map) {
        List<ProductInfoResponsePo> configs = (List<ProductInfoResponsePo>) map.get("list");
        StringBuilder sb = new StringBuilder();
        sb.append("update eboos_product set");
        sb.append(" product_name=case product_code ");

        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getProductName()+"'");
        }
        sb.append(" end ,");
        sb.append(" payment_type=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getPaymentType()+"'");
        }
        sb.append(" end ,");

        sb.append(" supplier_settlement_price=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getCalendarPrices().get(0).getSupplierSettlementPrice()+"'");
        }
        sb.append(" end ,");

        sb.append(" instruction=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getInstruction()+"'");
        }
        sb.append(" end ,");
        //是否实名制
        sb.append(" is_real_name=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then ");
            sb.append(configs.get(i).getIsRealName()+"");
        }
        sb.append(" end ");


 /*       sb.append(" refund_model=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(((configs.get(i).getCanRefund().equals("TRUE")) ? "AVAILABLE_FOR_NO_CONSUMPTION" : "NOT_REFUNDABLE")+"'");
        }
        sb.append(" end ");*/

        sb.append(" where product_code in");
        sb.append("(");
        for (int i = 0; i < configs.size(); i++) {
            sb.append("'"+configs.get(i).getProductCode()+"'");
            if (i < configs.size() - 1) {
                sb.append(",");
            }
        }
        sb.append(")");
        logger.info(sb.toString());
        return sb.toString();
    }

    /**
     * @ClassName: ProductProvider
     * @param map
     * @Return: java.lang.String
     * @Decription: 畅游通门票价格修改的集合
     * @CreateDate: Created in 2018/3/27 0027 上午 11:52
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public String updateTicketPrice(Map map){
        List<ProductInfoResponsePo> configs = (List<ProductInfoResponsePo>) map.get("list");
        StringBuilder sb = new StringBuilder();
        sb.append("update eboos_ticket_price set");
        sb.append(" settlement_price=case product_code ");

        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then  '");
            sb.append(configs.get(i).getCalendarPrices().get(0).getSupplierSettlementPrice()+"'");
        }
        sb.append(" end ,");
        sb.append(" sales_price=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getCalendarPrices().get(0).getMarketPrice()+"'");
        }
        sb.append(" end ");
        sb.append(" where product_code in");
        sb.append("(");
        for (int i = 0; i < configs.size(); i++) {
            sb.append("'"+configs.get(i).getProductCode()+"'");
            if (i < configs.size() - 1) {
                sb.append(",");
            }
        }
        sb.append(")");
        logger.info("畅游通门票价格修改的集合sql为："+sb.toString());
        return sb.toString();
    }
    /**
     * @param productString
     * @ClassName: ProductProvider
     * @Return: java.lang.String
     * @Decription: 畅游通公共产品修改的集合
     * @CreateDate: Created in 2018/3/15 0015 下午 4:19
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public String updateProduct(String productString) {
        List<ProductInfoResponsePo> productInfoList = JSONObject.parseArray(productString, ProductInfoResponsePo.class);
        StringBuffer stringBufferTotal = new StringBuffer();
        for (int i = 0; i < productInfoList.size(); i++) {
            StringBuffer stringBuffer = new StringBuffer("update eboos_product set ");
            int count = 0;
            if (!("".equals(productInfoList.get(i).getProductName()) || null == productInfoList.get(i).getProductName())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }

                stringBuffer.append("product_name='" + productInfoList.get(i).getCity() + "'");
                count++;
            }
            if (!(null == productInfoList.get(i).getPaymentType() || "".equals(productInfoList.get(i).getPaymentType()))) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("payment_type='" + productInfoList.get(i).getPaymentType() + "'");
                count++;
            }
            if (!(null == productInfoList.get(i).getInstruction() || "".equals(productInfoList.get(i).getInstruction()))) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("instruction='" + productInfoList.get(i).getInstruction() + "'");
                count++;
            }
            if (!(null == productInfoList.get(i).getCanRefund() || "".equals(productInfoList.get(i).getCanRefund()))) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("refund_model='" + (productInfoList.get(i).getCanRefund().equals("true") ? "1" : "2") + "'");
                count++;
            }
            stringBuffer.append(" where 1=1 and product_code='" + productInfoList.get(i).getProductCode() + "';");
            stringBufferTotal.append(stringBuffer);
        }

        logger.info(stringBufferTotal.toString());
        return stringBufferTotal.toString();
    }


    /**
     * @ClassName: ProductProvider
     * @param map
     * @Return: java.lang.String
     * @Decription: 畅游通门票修改的集合
     * @CreateDate: Created in 2018/3/16 0016 下午 1:49
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public String updateTicketAll(Map map) {

        List<ProductInfoResponsePo> configs = (List<ProductInfoResponsePo>) map.get("list");
        StringBuilder sb = new StringBuilder();
        sb.append("update eboos_ticket set");

        sb.append(" city=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getCity()+"'");
        }
        sb.append(" end ,");

        sb.append(" name=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append("  when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getProductName()+"'");
        }
        sb.append(" end ,");

        sb.append(" payment_type=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append("  when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getPaymentType()+"'");
        }
        sb.append(" end ,");

        //是否实名制
        sb.append(" is_real_name=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append("  when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then ");
            sb.append(configs.get(i).getIsRealName()+"");
        }
        sb.append(" end ,");

        sb.append(" book_advance_day=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getBookAdvanceDay()+"'");
        }
        sb.append(" end ,");

        sb.append(" book_advance_time=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getBookAdvanceTime()+"'");
        }
        sb.append(" end ,");

        sb.append(" use_advance_hour=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getUseAdvanceHour()+"'");
        }
        sb.append(" end ,");

        sb.append(" days_after_use_date_valid=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getDaysAfterUseDateValid()+"'");
        }
        sb.append(" end ,");

        sb.append(" book_person_type=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getBookPersonType()+"'");
        }
        sb.append(" end ,");

        sb.append(" visit_person_required_for_quantity=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getVisitPersonRequiredForQuantity()+"'");
        }
        sb.append(" end ,");

        sb.append(" valid_type=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getValidType()+"'");
        }
        sb.append(" end ,");

        sb.append(" auto_cancel_time=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getAutoCancelTime()+"'");
        }
        sb.append(" end ,");

        sb.append(" instruction=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append("  when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getInstruction()+"'");
        }
        sb.append(" end ,");

        sb.append(" can_refund=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append("  when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append((configs.get(i).getCanRefund())+"'");
        }
        sb.append(" end ,");

        sb.append(" can_overdue_refund=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getCanOverdueRefund()+"'");
        }
        sb.append(" end ,");


        sb.append(" refund_apply_time_before_valid_end_day=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getRefundApplyTimeBeforeValidEndDay()+"'");
        }
        sb.append(" end ,");


        sb.append(" refund_charge_type=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getRefundChargeType()+"'");
        }
        sb.append(" end ,");

        sb.append(" refund_info=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getRefundInfo()+"'");
        }
        sb.append(" end ,");

        sb.append(" sms_templet=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getSmsTemplet()+"'");
        }
        sb.append(" end ,");


        sb.append(" e_ticket_type=case product_code ");
        for (int i = 0; i < configs.size(); i++) {
            sb.append(" when ");
            sb.append(configs.get(i).getProductCode());
            sb.append(" then '");
            sb.append(configs.get(i).getETicketType()+"'");
        }
        sb.append(" end ");

        sb.append(" where product_code in");
        sb.append("(");
        for (int i = 0; i < configs.size(); i++) {
            sb.append("'"+configs.get(i).getProductCode()+"'");
            if (i < configs.size() - 1) {
                sb.append(",");
            }
        }
        sb.append(")");
        logger.info(sb.toString());
        return sb.toString();
    }
    /**
     * @param productString
     * @ClassName: ProductProvider
     * @Return: java.lang.String
     * @Decription: 畅游通门票修改的集合
     * @CreateDate: Created in 2018/3/15 0015 下午 4:19
     * @Author: <a href="410508961@qq.com">夏凯</a>
     * @Modify:
     */
    public String updateTicket(String productString) {

        List<ProductInfoResponsePo> productInfoList = JSONObject.parseArray(productString, ProductInfoResponsePo.class);
        StringBuffer stringBufferTotal = new StringBuffer();
        for (int i = 0; i < productInfoList.size(); i++) {
            StringBuffer stringBuffer = new StringBuffer("update eboos_ticket set ");
            int count = 0;
            if (!("".equals(productInfoList.get(i).getCity()) || null == productInfoList.get(i).getCity())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("city='" + productInfoList.get(i).getCity() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getProductName()) || null == productInfoList.get(i).getProductName())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("name='" + productInfoList.get(i).getProductName() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getPaymentType()) || null == productInfoList.get(i).getPaymentType())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("payment_type='" + productInfoList.get(i).getPaymentType() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getBookAdvanceDay()) || null == productInfoList.get(i).getBookAdvanceDay())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("book_advance_day=" + productInfoList.get(i).getBookAdvanceDay());
                count++;
            }
            if (!("".equals(productInfoList.get(i).getBookAdvanceTime()) || null == productInfoList.get(i).getBookAdvanceTime())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("book_advance_time='" + productInfoList.get(i).getBookAdvanceTime() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getUseAdvanceHour()) || null == productInfoList.get(i).getUseAdvanceHour())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("use_advance_hour=" + productInfoList.get(i).getUseAdvanceHour());
                count++;
            }
            if (!("".equals(productInfoList.get(i).getDaysAfterUseDateValid()) || null == productInfoList.get(i).getDaysAfterUseDateValid())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("days_after_use_date_valid=" + productInfoList.get(i).getDaysAfterUseDateValid());
                count++;
            }
            if (!("".equals(productInfoList.get(i).getBookPersonType()) || null == productInfoList.get(i).getBookPersonType())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("book_person_type='" + productInfoList.get(i).getBookPersonType() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getVisitPersonRequiredForQuantity()) || null == productInfoList.get(i).getVisitPersonRequiredForQuantity())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("visit_person_required_for_quantity=" + productInfoList.get(i).getVisitPersonRequiredForQuantity());
                count++;
            }
            if (!("".equals(productInfoList.get(i).getValidType()) || null == productInfoList.get(i).getValidType())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("valid_type='" + productInfoList.get(i).getValidType() + "'");
                count++;
            }

            if (!("".equals(productInfoList.get(i).getAutoCancelTime()) || null == productInfoList.get(i).getAutoCancelTime())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("auto_cancel_time=" + productInfoList.get(i).getAutoCancelTime());
                count++;
            }
            if (!("".equals(productInfoList.get(i).getInstruction()) || null == productInfoList.get(i).getInstruction())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("instruction='" + productInfoList.get(i).getInstruction() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getCanRefund()) || null == productInfoList.get(i).getCanRefund())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("can_refund='" + productInfoList.get(i).getCanRefund() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getCanOverdueRefund()) || null == productInfoList.get(i).getCanOverdueRefund())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("can_overdue_refund='" + productInfoList.get(i).getCanOverdueRefund() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getRefundApplyTimeBeforeValidEndDay()) || null == productInfoList.get(i).getRefundApplyTimeBeforeValidEndDay())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("refund_apply_time_before_valid_end_day='" + productInfoList.get(i).getRefundApplyTimeBeforeValidEndDay() + "'");
                count++;
            }

            if (!("".equals(productInfoList.get(i).getRefundChargeType()) || null == productInfoList.get(i).getRefundChargeType())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("refund_charge_type='" + productInfoList.get(i).getRefundChargeType() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getRefundInfo()) || null == productInfoList.get(i).getRefundInfo())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("refund_info='" + productInfoList.get(i).getRefundInfo() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getSmsTemplet()) || null == productInfoList.get(i).getSmsTemplet())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("sms_templet='" + productInfoList.get(i).getSmsTemplet() + "'");
                count++;
            }
            if (!("".equals(productInfoList.get(i).getETicketType()) || null == productInfoList.get(i).getETicketType())) {
                if (count != 0) {
                    stringBuffer.append(",");
                }
                stringBuffer.append("e_ticket_type='" + productInfoList.get(i).getETicketType() + "'");
                count++;
            }
            stringBuffer.append(" where 1=1 and product_code='" + productInfoList.get(i).getProductCode() + "';");
            stringBufferTotal.append(stringBuffer);
        }
        logger.info(stringBufferTotal.toString());
        return stringBufferTotal.toString();
    }
}
